
import java.io.File;
import java.util.ArrayList;
import java.util.Hashtable;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

@SuppressWarnings("rawtypes")
public class ProcessFiles<T extends Driver> {

    public static Graph processGraph(File netFile) {

        Hashtable<String, Vertex> V = new Hashtable<String, Vertex>();
        Hashtable<String, Edge> E = new Hashtable<String, Edge>();

        try {

            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(netFile);

            //Create vertices
            NodeList list = doc.getElementsByTagName("node");
            for (int i = 0; i < list.getLength(); i++) {
                Element e = (Element) list.item(i);
                V.put(e.getAttribute("id"), new Vertex(e.getAttribute("id")));
            }

            //Create edges
            list = doc.getElementsByTagName("edge");
            for (int i = 0; i < list.getLength(); i++) {
                Element e = (Element) list.item(i);

                if (Params.ORTUZAR_FUNCTION) {
                    E.put(e.getAttribute("id"), new Edge(
                            e.getAttribute("id"),
                            V.get(e.getAttribute("from")),
                            V.get(e.getAttribute("to")),
                            Float.parseFloat(e.getAttribute("weight")),
                            false //directed
                            , 0, 0, 0
                    ));
                } else {
                    E.put(e.getAttribute("id"), new Edge(
                            e.getAttribute("id"),
                            V.get(e.getAttribute("from")),
                            V.get(e.getAttribute("to")),
                            Float.parseFloat(e.getAttribute("weight")),
                            true //directed
                            , Float.parseFloat(e.getAttribute("fftime")),
                            Float.parseFloat(e.getAttribute("power")),
                            Float.parseFloat(e.getAttribute("beta"))
                    ));
                }
            }

        } catch (Exception e) {
            System.err.println("Error on reading XML file!");
            e.printStackTrace();
        }

        return new Graph(V, E, true);
    }

    @SuppressWarnings("unchecked")
    public static <T> ArrayList<T> processODMatrix(Graph G, File odFile) {//, Class<T> t) {

        ArrayList<T> D = new ArrayList<T>();

        try {

            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(odFile);

            //Create entries
            long countD = 0;
            NodeList list = doc.getElementsByTagName("od");
            for (int i = 0; i < list.getLength(); i++) {

                Element e = (Element) list.item(i);

                Vertex origin = G.getVertex(e.getAttribute("origin"));
                Vertex destination = G.getVertex(e.getAttribute("destination"));

                //create as many drivers as the number of trips defined
                for (long d = Long.parseLong(e.getAttribute("trips")); d > 0; d--) {
                    QLearningDriver abc = new QLearningDriver(++countD, origin, destination, G);
                    D.add((T) abc);
                }
            }

        } catch (Exception e) {
            System.err.println("Error on reading XML file!");
            e.printStackTrace();
        }

        return D;
    }

}
